package com.mdd.front.controller;

import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.front.service.IAddressService;
import com.mdd.front.validate.commons.PageValidate;
import com.mdd.front.validate.order.AddressCreateValidate;
import com.mdd.front.validate.order.AddressUpdateValidate;
import com.mdd.front.vo.order.AddressVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 地址管理
 *
 * @author shenjian
 * @createTime 2023年02月05日 23:19:00
 */
@RestController
@RequestMapping("api/address")
public class AddressController {

    @Resource
    IAddressService iAddressService;

    /**
     * 地址列表
     */
    @GetMapping("/list")
    public AjaxResult<PageResult<AddressVo>> list(@Validated PageValidate pageValidate) {
        PageResult<AddressVo> list = iAddressService.list(pageValidate);
        return AjaxResult.success(list);
    }

    /**
     * 查询地址详情
     */
    @GetMapping("/detail")
    public AjaxResult<AddressVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        return AjaxResult.success(iAddressService.detail(id));
    }

    /**
     * 新增地址
     */
    @PostMapping("/add")
    public AjaxResult<?> add(@Validated @RequestBody AddressCreateValidate createValidate) {
        iAddressService.add(createValidate);
        return AjaxResult.success();
    }

    /**
     * 更新地址
     */
    @PostMapping("/edit")
    public AjaxResult<?> update(@Validated @RequestBody AddressUpdateValidate updateValidate) {
        iAddressService.edit(updateValidate);
        return AjaxResult.success();
    }

    /**
     * 删除地址
     */
    @GetMapping("/del")
    public AjaxResult<?> delete(@Validated @IDMust() @RequestParam("id") Integer id) {
        iAddressService.del(id);
        return AjaxResult.success();
    }

    /**
     * 设置默认地址
     */
    @GetMapping("/set-default")
    public AjaxResult<?> setDefault(@Validated @IDMust() @RequestParam("id") Integer id) {
        iAddressService.setDefault(id);
        return AjaxResult.success();
    }

    /**
     * 获取首页的地址
     */
    @GetMapping("/index-address")
    public AjaxResult<?> index(@RequestParam(value = "addressId", required = false) Integer addressId,
                               @RequestParam(value = "longitude", required = false) Double longitude,
                               @RequestParam(value = "latitude", required = false) Double latitude) {
        return AjaxResult.success(iAddressService.getIndexAddressInfo(addressId, longitude, latitude));
    }
}
